<?php

namespace AuctionBack\Service;

require_once ROOT_DIR . '/App/AuctionBack/Org/Thrift/Lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
use Services\BailService\BailServiceClient;

$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', MODULE_PATH . 'Org/Thrift/Lib');
$loader->registerDefinition('Services', MODULE_PATH . 'Org/Thrift/gen-php');
$loader->register();

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TMultiplexedProtocol;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use Thrift\Transport\THttpClient;

class ThriftBailService
{


    public function __construct($app_url = '')
    {
        if (empty($app_url)) {
            $app_url = C('ANT_NEST.WEAVER_URL');
        }
        $handle = new THttpClient($app_url, 80, '/Bail/run');
        $transport = new TBufferedTransport($handle);
        $protocol = new TBinaryProtocol($transport);
        $bailProtocol = new TMultiplexedProtocol($protocol, 'BailService');
        $this->bailService = new BailServiceClient($bailProtocol);
    }

    /**获取车商保证金信息
     * @param $dealer_id
     * @return array|\Services\BailService\Bail
     */
    public function getDealerBail($dealer_id)
    {
        try {
            $result = $this->bailService->getDealerBail($dealer_id);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'message' => $tx->getMessage());
        }
    }


    /**减少保证金
     * @param $dealer_id
     * @param $amount
     * @param $useType
     * @param $useDetail
     * @return array|\Services\BailService\Bail
     */
    public function decBail($dealer_id, $amount, $useType, $useDetail, $orderId, $rechargeMethod = 0, $payMethodId = 0, $collectionId = 0)
    {
        try {
            $result = $this->bailService->decBail($dealer_id, $amount, $useType, $useDetail, $orderId, $rechargeMethod, $payMethodId, $collectionId);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'message' => $tx->getMessage());
        }
    }

    /**增加保证金
     * @param $dealer_id
     * @param $amount
     * @param $useType
     * @param $useDetail
     */
    public function incBail($dealer_id, $amount, $useDetail, $rechargeNo = '', $rechargeId = 0, $rechargeMethod = 0, $collectionId = 0)
    {
        try {
            $result = $this->bailService->incBail($dealer_id, $amount, $useDetail, $rechargeNo, $rechargeId, $rechargeMethod, $collectionId);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'message' => $tx->getMessage());
        }
    }

    /**冻结保证金
     * @param $dealer_id
     * @param $amount
     * @param $order_id
     * @return array|\Services\BailService\Bail
     */
    public function freezeBail($dealer_id, $amount, $order_id)
    {

        try {
            $result = $this->bailService->freezeBail($dealer_id, $amount, $order_id);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'message' => $tx->getMessage());
        }
    }

    /**
     * @param $dealer_id
     * @param $amount
     * @param $order_id
     * @return array|\Services\BailService\Bail
     */
    public function unfreezeBail($dealer_id, $order_id)
    {
        try {
            $result = $this->bailService->unfreezeBail($dealer_id, $order_id);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'message' => $tx->getMessage());
        }
    }

    /**
     * @param $dealer_id
     * @param $amount
     * @param $order_id
     * @param $decAmount
     * @param $useType
     * @param $useDetail
     * @return array|\Services\BailService\Bail
     */
    public function unfreezeDecBail($dealer_id, $order_id, $decAmount, $useType, $useDetail)
    {
        try {
            $result = $this->bailService->unfreezeDecBail($dealer_id, $order_id, $decAmount, $useType, $useDetail);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'message' => $tx->getMessage());
        }
    }

    /**
     * 保证金日志列表
     * @param $dealer_id
     * @param $use_type
     * @param $use_time
     * @param $page
     * @param $page_size
     * @param $is_unfree
     * @param $pay_method
     * @return array|\Services\BailService\Bail
     */
    public function getBailLogsByDealerId($dealer_id, $use_type, $use_time, $page, $page_size, $is_unfree, $pay_method)
    {
        try {
            $result = $this->bailService->getBailLogsByDealerId($dealer_id, $use_type, $use_time, $page, $page_size, $is_unfree, $pay_method);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'message' => $tx->getMessage());
        }
    }
}